<?php
namespace Umpadmin\Controller;
use Think\Controller;
class EventcfgController extends ExtendController {
	public function check_name(){
		$name = I("active_name");
		$desc = I("desc");
		$CAT = M("PromoCats");
		$PRO = M("Promosions");
		$res_pro = $PRO->where("`sid`=".$this->sid." AND `name`='".$name."'")->count("id");
		$res_cat = $CAT->where("`sid`=".$this->sid." AND `name`='".$name."'")->count("id");
		if($res_pro || $res_cat){
			echo ("与已有的活动名称重复！");
			die();
		}
		Vendor('taobaosdk.top.request.MarketingPromotionKfcRequest');
		$req = new \MarketingPromotionKfcRequest;
		$req->setPromotionTitle($name);
		$req->setPromotionDesc($desc?$desc:$name);
		$resp = $this->c->execute($req, session("access_token"));
		if(!$resp->is_success){
			echo ($resp->sub_msg);
		}
	}
	public function get_cats(){
		$start = I("start");
		$limit = I("limit");
		$CAT = M("PromoCats");
		$results = $CAT->where("`sid`=".$this->sid)->count("id");
		$cats = $CAT->where("`sid`=".$this->sid)->limit($start.",".$limit)->getField("id,name,start_time,end_time");
		$data["rows"]=$cats;
		$data["results"]=$results;
		$this->ajaxReturn($data);
	}
	public function load_adv_items(){
		$eid = I("eid");
		$id = I("id");
		$PRO = M("Promosions");
		$itemids = $PRO->where("`sid`=".$this->sid." AND `id`=".$id)->limit(1)->getField("item_iids");
		if($itemids){
			$itemids = explode(",",$itemids);
			Vendor('taobaosdk.top.request.ItemsSellerListGetRequest');
			$req = new \ItemsSellerListGetRequest;
			$req->setFields("num_iid,title,pic_url");
			$req->setNumIids(implode($itemids,","));
			$items_resp = $this->c->execute($req, session("access_token"));
			$resp = new \stdClass();
			if($items_resp->items->item){
				foreach($items_resp->items->item as $k=>$item){
					$resp->promotion_range_list->promotion_range[$k]->item_title=$item->title;
					$resp->promotion_range_list->promotion_range[$k]->pic_url=$item->pic_url;
					$resp->promotion_range_list->promotion_range[$k]->item_id=$item->num_iid;
				}
			}else{
				$resp->error="错误！当前商品并不在出售中，请重新设置。";
			}
			
			$resp->itemids = $itemids;
			$resp->allcount = count($itemids);
		}else{
			$resp->allcount = 0;
		}
		$this->ajaxReturn($resp);
	}
	public function save_adv_items(){
		$eid = I("eid");
		$iids = I("iids");
		$PRO = M("Promosions");
		$re = $PRO->where("`id`=".$eid." AND `sid`=".$this->sid)->save(Array("item_iids"=>$iids));
		$this->ajaxReturn(Array("issuccess"=>$re));
	}
	public function save_adv_rule(){
		$eid = I("eid");
		$id = I("id");
		$val = I("value");
		$start_time = I("start_time");
		$end_time = I("end_time");
		$adv_tp = I("adv_tp");
		$set_event_time = I("set_event_time");
		$set_cat_time = I("set_cat_time");
		$event_name = I("event_name");
		$decrease_amount = I("decrease_amount");
		$friend_num = I("friend_num");
		$tag = I("tag");
		$PRO = M("Promosions");
		if($set_cat_time=="true"){
			$CAT = M("PromoCats");
			$cat_data["start_time"] = $start_time?$start_time:null;
			$cat_data["end_time"] = $end_time?$end_time:null;
			$re = $CAT->where("`id`=".$id." AND `sid`=".$this->sid)->save($cat_data);
			$this->ajaxReturn(Array("issuccess"=>$re));
			die();
		}
		if($adv_tp=="pay_time"){
			if($set_event_time=="true"){
				Vendor('taobaosdk.top.request.PromotionmiscItemActivityUpdateRequest');
				if($start_time){$data["start_time"] = $start_time;}
				if($end_time){$data["end_time"] = $end_time;}
				if($friend_num){$data["friend_num"] = $friend_num;}
				if($decrease_amount){$data["decrease_amount"] = $decrease_amount;}
				$req = new \PromotionmiscItemActivityUpdateRequest;
				$req->setActivityId($eid);
				$req->setName($event_name);
				$req->setParticipateRange("1");
				$req->setStartTime($start_time);
				$req->setEndTime($end_time);
				$req->setIsUserTag("true");
				$req->setUserTag("NEW_".$tag);
				$req->setIsDecreaseMoney("true");
				$req->setDecreaseAmount($decrease_amount);
				$req->setIsDiscount("false");
				$req->setDiscountRate("100");
				$resp = $this->c->execute($req, session("access_token"));
				if($resp->code){
					$this->ajaxReturn(Array("issuccess"=>0,"resp"=>$resp));
					die();
				}
			}else{
				$data["pay_time_start"] = $start_time?$start_time:null;
				$data["pay_time_end"] = $end_time?$end_time:null;
			}
			$re = $PRO->where("`id`=".$id." AND `sid`=".$this->sid)->save($data);
		}else{
			$re = $PRO->where("`id`=".$id." AND `sid`=".$this->sid)->save(Array($adv_tp=>$val));
		}
		$this->ajaxReturn(Array("issuccess"=>$re));
	}
	public function set_invent_prom(){
		$id = I("prom_id");
		$cid = I("cid");
		$PRO = M("Promosions");
		$is_inv = $PRO->where("`sid`=".$this->sid." AND `is_invite`=1 AND `act_cat_id`=".$cid)->getField("`id`");
		if($is_inv){
			$data["msg"]="已经有别的被邀请人折扣，请先取消。";
		}else{
			$data["is_success"] = $PRO->where("`sid`=".$this->sid." AND `id`=".$id)->save(Array("is_invite"=>1));
		}
		$this->ajaxReturn($data,"JSON");
	}
	public function remove_invent_prom(){
		$id = I("prom_id");
		$PRO = M("Promosions");
		$data["is_success"] = $PRO->where("`sid`=".$this->sid." AND `id`=".$id)->save(Array("is_invite"=>0));
		if(!$data["is_success"]){$data["msg"]="这不是被邀请人折扣";}
		$this->ajaxReturn($data,"JSON");
	}
}